import { configureStore, Middleware, Reducer } from '@reduxjs/toolkit';
import createReducerManager from '@/stores/createReducerManager';
import { store } from './stores';
export interface IReducers {
  [key: string]: Reducer;
}
// 定义一个configStore函数，接收两个参数：reducers和middleware
const configStore = (reducers: IReducers, middleware: Middleware[]) => {
  // 创建一个reducerManager，传入reducers
  const reducerManager = createReducerManager({ ...reducers });
  const internalStore = configureStore({
    reducer: reducerManager.reduce,
    middleware,
  });
  type TStore = typeof internalStore;
  // 定义一个接口IStore，继承自TStore，并添加reducerManager属性
  interface IStore extends TStore {
    reducerManager: ReturnType<typeof createReducerManager>;
  }
  // 将internalStore转换为IStore类型
  const store = internalStore as IStore;
  // 将reducerManager赋值给store的reducerManager属性
  store.reducerManager = reducerManager;
  return store;
};

export default configStore;
